package main;

import java.awt.Button;
import java.awt.Color;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// Roll two dice for a courage test
public class CourageRoll extends DieRoll {
	
	private Button useWill;
	private int willUsed = 0;

	public CourageRoll(Unit unitRolling, int x, int y, int modifier,
			String note, UI myUI) {
		super(unitRolling, x, y, modifier, note, myUI);
	}

	public CourageRoll(Unit unitRolling, int x, int y, String note, UI myUI) {
		super(unitRolling, x, y, note, myUI);
	}
	
	protected void rollDie() {
		int d1 = rng.nextInt(6) + 1;
		int d2 = rng.nextInt(6) + 1;
		dieValue = d1 + d2 + modifier;
		dieValue = Math.min(dieValue, 12);
		dieValue = Math.max(dieValue, 2);
		valueLabel = new Label(dieValue + " ");
		valueLabel.setName("dieRoll");
		valueLabel.setBounds(x,y,20,20);
		myUI.add(valueLabel);
	}

	protected void createButton() {
		System.out.println("Creating courageRoll buttons");
		
		useMight = new Button("+Might");
		useMight.setName("dieRoll");
		useMight.setBounds(x+20,y-2,50,12);
		useMight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mightUsed += 1;
				unitRolling.might -= 1;
				valueLabel.setText((dieValue + mightUsed + willUsed) + " ");
				for (DieRoll roll : myUI.diceOnTable)
					roll.update();
				myUI.revalidate();
				myUI.repaint();
			}
		});
		myUI.add(useMight);
		useWill = new Button("+Will");
		useWill.setName("dieRoll");
		useWill.setBounds(x+20,y+10,50,12);
		useWill.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				willUsed += 1;
				unitRolling.will -= 1;
				valueLabel.setText((dieValue + mightUsed + willUsed) + " ");
				for (DieRoll roll : myUI.diceOnTable)
					roll.update();
				myUI.revalidate();
				myUI.repaint();
			}
		});
		myUI.add(useWill);
		
		if (this.getRoll() >= 12 || unitRolling.might <= 0)
			useMight.setEnabled(false);
		if (this.getRoll() >= 12 || unitRolling.will <= 0)
			useWill.setEnabled(false);
		
		if (note != "") {
			noteLabel = new Label(note);
			noteLabel.setName("dieRoll");
			noteLabel.setBounds(x+80, y, 100, 20);
			noteLabel.setBackground(new Color(255,255,255,0));
			myUI.add(noteLabel);
		}
		
		myUI.revalidate();
		myUI.repaint();
	}
	

	public int getRoll() {
		return dieValue + mightUsed + willUsed;
	}
	
	public boolean isModifiable() {
		return (useMight.isEnabled() || useWill.isEnabled());
	}
	
	public void cleanup(){
		System.out.println("Cleaning up DieRoll for " + unitRolling.toChoiceString());
		myUI.remove(valueLabel);
		myUI.remove(useMight);
		myUI.remove(useWill);
		if (note != "")
			myUI.remove(noteLabel);
	}
	
	public void reroll(){
		this.mightUsed = 0;
		this.willUsed = 0;
		this.cleanup();
		this.rollDie();
		this.createButton();
	}
	
	public void update(){
		if (this.getRoll() >= 12 || unitRolling.might <= 0)
			useMight.setEnabled(false);
		if (this.getRoll() >= 12 || unitRolling.will <= 0)
			useWill.setEnabled(false);
	}
	
	public String toString() {
		return Integer.toString(this.getRoll());
	}
	
	public String toFullString() {
		String repr = "Unit rolling: " + this.unitRolling.toChoiceString() + "\n";
		repr += "Original roll (+modifier): " + this.dieValue + "\n";
		repr += "Modifier: " + this.modifier + "\n";
		repr += "Might used: " + this.mightUsed + "\n";
		repr += "Will used: " + this.mightUsed + "\n";
		return repr;
	}
}
